﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace HotelManagementClient.Utlis
{
    public class HttpGet
    {
        public static string des3Key = "Tt3rLPrDIVIhXqAz";// 长度控制为16，作为3DES加密用的key
        public static string Get(string url)
        {
            if(NetWorkUtil.CheckUrlVisit(url))
            {
                //创建请求
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

                string accesstoken = Encryptor.DecryptData(AppConfigUtil.GetValue("LoginUser", "accessToken"), des3Key);
                if (!string.IsNullOrEmpty(accesstoken))
                {
                    //添加请求头
                    request.Headers.Add("Authorization", "Bearer " + accesstoken);
                }

                //GET请求
                request.Method = "GET";
                request.ReadWriteTimeout = 5000;
                request.ContentType = "application/json;charset=UTF-8";
                try
                {
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();//执行get请求
                    Stream myResponseStream = response.GetResponseStream();
                    StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));

                    //返回内容JSON
                    string retString = myStreamReader.ReadToEnd();
                    return retString;
                }
                catch (Exception ex)
                {
                    WriteLog.Write(1,ex.Message);
                    return "";
                }
                
            }
            else
            {
                return "";
            }
            
        }
    }
}
